package com.cyyang.shop.controller;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cyyang.shop.bean.Sku;
import com.cyyang.shop.bean.SkuVo;
import com.cyyang.shop.bean.Spec;
import com.cyyang.shop.bean.SpecOption;
import com.cyyang.shop.bean.Spu;
import com.cyyang.shop.service.SkuService;
import com.cyyang.shop.service.SpecService;
import com.cyyang.shop.service.SpuService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("sku")
public class SkuController {
	
	@Value("${uploadPath}")
	private String uploadPath;

	@Reference
	SkuService skuService;
	
	@Reference
	SpuService spuService;
	
	@Reference
	SpecService specService;
	
	
	
	@RequestMapping("findSpecOptionById")
	@ResponseBody
	public List<SpecOption> getSpecOptionBySpecId(Integer specId) {
		return specService.findSpecOptionBySpecId(specId);
	}
	
	@RequestMapping("toUpdate")
	public String toUpdate(Integer skuId,Model m) {
		Sku sku = skuService.findSkuBySkuId(skuId);
		PageInfo<Spec> list = specService.list(1, Integer.MAX_VALUE);
		m.addAttribute("specList", list.getList());
		m.addAttribute("sku",sku);
		return "sku/update";
	}
	//即是添加方法,也是修改方法
	@RequestMapping("add")
	@ResponseBody
	public boolean add(@RequestParam("optionId")Integer[] optionIds
			,@RequestParam("specId")Integer[] specIds,
			Sku sku,MultipartFile cartThumbnailFile,MultipartFile imageFile) {
		try {
			if(sku!=null && sku.getId()!=null) {
				//说明是修改
				String cartThumbnailFilePath = upload(cartThumbnailFile);
				String imageFilePath = upload(imageFile);
				if(cartThumbnailFilePath!=null) {
					sku.setCartThumbnail(cartThumbnailFilePath);
				}
				if(imageFilePath!=null) {
					sku.setImage(imageFilePath);
				}
				skuService.update(sku,specIds,optionIds);
				return true;
			}
			System.out.println("optionIds="+Arrays.toString(optionIds));
			System.out.println("specIds="+Arrays.toString(specIds));
			String cartThumbnailFilePath = upload(cartThumbnailFile);
			String imageFilePath = upload(imageFile);
			sku.setCartThumbnail(cartThumbnailFilePath);
			sku.setImage(imageFilePath);
			skuService.add(sku,specIds,optionIds);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	//图片上传的方法
		private String upload(MultipartFile filePic) {
			String newName= null;
			if(filePic!=null && !filePic.isEmpty()) {
				String originalFilename = filePic.getOriginalFilename();//aaa.jpg
				//获取文件的后缀名
				String subName = originalFilename.substring(originalFilename.lastIndexOf("."));//.jpg
				//为该文件随机一个名称
				String preName = UUID.randomUUID().toString();//asdflasdkfjldskasdf
				//拼接新的文件名
				 newName = preName+subName;
				File dest = new File(uploadPath, newName);
				try {
					filePic.transferTo(dest);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
			return newName;
		}
	
	@RequestMapping("toAdd/{spuId}")
	public String toAdd(Model m,@PathVariable("spuId") int spuId) {
		Spu spu = spuService.finSpuBySpuId(spuId);
		PageInfo<Spec> specs = specService.list(1, Integer.MAX_VALUE);
		m.addAttribute("spu", spu);
		m.addAttribute("specs", specs.getList());
		
		return "sku/add";
	}
	
	@RequestMapping("list")
	public String list(SkuVo vo,Model m,@RequestParam(defaultValue="1")int pageNum,@RequestParam(defaultValue="5")int pageSize) {
		PageInfo<Sku> info = skuService.list(vo, pageNum, pageSize);
		m.addAttribute("info", info);
		return "sku/list";
	}
	
	@RequestMapping("delById")
	@ResponseBody
	public boolean delById(@RequestParam("ids[]")Integer ...ids) {
		try {
			skuService.delById(ids);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}
